<template>
    <div>
        <!-- 搜索栏 -->
        <div style="margin: 10px 0;">
            <el-input placeholder="请输入用户名" style="width: 200px;margin-right: 5px;"
                      suffix-icon="el-icon-search" v-model="user.username"></el-input>
            <el-input placeholder="请输入昵称" style="width: 200px;margin-right: 5px;"
                      suffix-icon="el-icon-search" v-model="user.nickname"></el-input>
            <el-input placeholder="请输入地址" style="width: 200px;margin-right: 5px;"
                      suffix-icon="el-icon-search" v-model="user.address"></el-input>
            <el-button type="primary" @click="getAll">搜索</el-button>
            <el-button type="primary" @click="clearQuery">清空</el-button>
        </div>
        <!--操作栏-->
        <div style="margin: 10px 0;">
            <el-button type="primary" @click="handleCreate">新增<i class="el-icon-circle-plus-outline"></i>
            </el-button>
            <el-button type="danger" @click="delBatch" style="margin-right: 10px">批量删除<i
                    class="el-icon-remove-outline"></i></el-button>
            <!--上传-->
            <el-popover
                    placement="right"
                    width="400"
                    trigger="click"
            >
                <el-button slot="reference" type="primary" style="margin-right: 10px">导入<i class="el-icon-bottom"></i>
                </el-button>
                <el-upload
                        accept="xlsx"
                        action="http://localhost:9090/users/import"
                        :on-success="handleExcelImportSuccess"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :before-remove="beforeRemove"
                        multiple
                        :limit="3"
                        :on-exceed="handleExceed"
                        :file-list="fileList"
                        :auto-upload="false"
                        ref="upload">
                    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                    <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器
                    </el-button>
                    <div slot="tip" class="el-upload__tip">只能上传xls/xlsx文件，且不超过500kb</div>
                </el-upload>
            </el-popover>
            <el-button type="primary" @click="expt">导出<i class="el-icon-top"></i></el-button>
        </div>
        <!--表格-->
        <el-table :data="tableData" :border="true" height="400" stripe
                  :header-cell-style="{background:'#eee'}"
                  ref="multipleTable"
                  @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="50" fixed="left">
            </el-table-column>
            <el-table-column prop="username" label="姓名" >
            </el-table-column>
            <el-table-column prop="nickname" label="昵称" >
            </el-table-column>
            <el-table-column prop="role" label="角色" >
                <template slot-scope="scope">
                    <el-tag type="primary" v-if="scope.row.role ==='ROLE_ADMIN'">管理员</el-tag>
                    <el-tag type="warning" v-if="scope.row.role ==='ROLE_TEACHER'">老师</el-tag>
                    <el-tag type="success" v-if="scope.row.role ==='ROLE_STUDENT'">学生</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="email" label="邮箱" >
            </el-table-column>
            <el-table-column prop="phone" label="电话" >
            </el-table-column>
            <el-table-column prop="address" label="地址">
            </el-table-column>
            <el-table-column label="操作" width="300">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="success"
                    @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i>
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row)">删除 <i class="el-icon-remove-outline"></i>
                    </el-button>
                    <!--@click="lookTeach(scope.row)">授课 -->
                    <el-button
                            size="mini"
                            type="primary"
                            v-if="scope.row.role==='ROLE_TEACHER'"
                            @click="lookTeach(scope.row.courses)">授课
                        <i class="el-icon-document"></i>
                    </el-button>
                    <el-button
                            size="mini"
                            type="primary"
                            v-if="scope.row.role==='ROLE_STUDENT'"
                            @click="lookChooseCourse(scope.row.chooseCourses)">已选
                        <i class="el-icon-document"></i>
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页控件 -->
        <div class="block">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[10, 20, 50]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
        <!--新增框-->
        <div>
            <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                <el-form :model="formData">
                    <!--<el-form-item label="ID" :label-width="formLabelWidth">
                        <el-input v-model="formData.id" autocomplete="off" disabled></el-input>
                    </el-form-item>-->
                    <el-form-item label="姓名" :label-width="formLabelWidth">
                        <el-input v-model="formData.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="昵称" :label-width="formLabelWidth">
                        <el-input v-model="formData.nickname" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="角色" :label-width="formLabelWidth">
                        <el-select clearable v-model="formData.role" placeholder="请选择角色" style="width: 100%">
                            <el-option v-for="item in roles" :label="item.name" :key="item.name" :value="item.tag"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="邮箱" :label-width="formLabelWidth">
                        <el-input v-model="formData.email" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="电话" :label-width="formLabelWidth">
                        <el-input v-model="formData.phone" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" :label-width="formLabelWidth">
                        <el-input v-model="formData.address" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handleAdd">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <!--编辑框-->
        <div>
            <el-dialog title="编辑用户" :visible.sync="dialogFormVisibleEdit">
                <el-form :model="formData">
                    <el-form-item label="ID" :label-width="formLabelWidth">
                        <el-input v-model="formData.id" autocomplete="off" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="姓名" :label-width="formLabelWidth">
                        <el-input v-model="formData.username" autocomplete="off" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="昵称" :label-width="formLabelWidth">
                        <el-input v-model="formData.nickname" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="角色" :label-width="formLabelWidth">
                        <el-select clearable v-model="formData.role" placeholder="请选择角色" style="width: 100%">
                            <el-option v-for="item in roles" :label="item.name" :key="item.name" :value="item.tag"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="邮箱" :label-width="formLabelWidth">
                        <el-input v-model="formData.email" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="电话" :label-width="formLabelWidth">
                        <el-input v-model="formData.phone" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" :label-width="formLabelWidth">
                        <el-input v-model="formData.address" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
                    <el-button type="primary" @click="handleAdd">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <div>
            <el-dialog title="授课信息" :visible.sync="vis" width="30%">
                <el-table :data="courses" border stripe>
                    <el-table-column prop="name" label="课程名称"></el-table-column>
                    <el-table-column prop="score" label="学分"></el-table-column>
                </el-table>
            </el-dialog>
        </div>
        <div>
            <el-dialog title="已选" :visible.sync="chooseVis" width="30%">
                <el-table :data="chooseCourses" border stripe>
                    <el-table-column prop="name" label="课程名称"></el-table-column>
                    <el-table-column prop="score" label="学分"></el-table-column>
                </el-table>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    import request from "../Api/request";

    export default {
        name: "UserView",
        data() {
            return {
                //分页数据
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 10,
                user: {
                    username: '',
                    nickname: '',
                    email: '',
                    phone: '',
                    address: '',
                },
                formData: {},
                dialogFormVisible: false,
                dialogFormVisibleEdit: false,
                formLabelWidth: '80px',
                multipleSelection: [],
                fileList: [],
                roles:[],
                courses:[],
                vis:false,
                chooseCourses:[],
                chooseVis:false,
            }
        },
        created() {
            this.getAll();
        },
        methods: {
            //分页
            getAll() {
                //PathVariable 不能使用params
                //request.get("http://localhost:9090/users/"+this.pageNum+"/"+this.pageSize).then((res)=>{
                this.request.get("/users/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        username: this.user.username,
                        nickname: this.user.nickname,
                        address: this.user.address,
                    }
                }).then((res) => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                });
                this.request.get("/roles").then(res=>{
                   this.roles=res.data;
                });
            },
            //清楚查询消息
            clearQuery() {
                this.user.username = "",
                    this.user.nickname = "",
                    this.user.address = "",
                    this.getAll()
            },
            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },
            //重置表单
            resetForm() {
                this.formData = {};
            },
            handleAdd(){
                this.request.post("/users",this.formData).then(res=>{
                    if (res.flag){
                        this.getAll();
                        this.dialogFormVisibleEdit=false;
                        this.dialogFormVisible=false;
                        this.$message.success("保存成功");
                    }
                }).catch(()=>{
                    this.$message.error("保存失败");
                })
            },
            //删除
            handleDelete(row) {
                this.$confirm("此操作永久删除当前信息，是否继续？", "提示", {type: "info"}).then(() => {//确认删除
                    this.request.delete("/users/" + row.id).then(res => {
                        if (res.flag) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("数据同步失败，自动刷新");
                        }
                    }).finally(() => {
                        this.getAll();
                    });
                }).catch(() => {//取消删除
                    this.$message.info("取消操作");
                });
            },
            //先查询，再编辑
            handleEdit(row) {
                this.request.get("/users/" + row.id).then(res => {
                    //if (res.data.flag && res.data.data != null)
                    if (res.flag) {
                        this.dialogFormVisibleEdit = true;
                        this.formData = row;
                    } else {
                        this.$message.error("数据同步失败，自动刷新");
                    }
                }).finally(() => {
                    this.getAll();
                })
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;//对象数组
            },
            //删除选中
            delBatch() {
                let ids = this.multipleSelection.map(value => value.id);//转化为id数组
                this.$confirm("此操作永久删除当前信息，是否继续？", "提示", {type: "info"}).then(() => {
                    this.request.post("/users/del/batch", ids).then(res => {
                        if (res.flag) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("数据同步失败，自动刷新");
                        }
                    }).finally(() => {
                        this.getAll();
                    })
                }).catch(() => {
                    this.$message.info("取消操作");
                })
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAll();
            },
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAll();
            },
            //导出
            expt() {
                window.open("http://localhost:9090/users/export")
            },
            //导入
            handleExcelImportSuccess(response, file, fileList) {
                console.log("re:" + response)//true
                this.fileList.push(file)
                if (response) {
                    this.$message.success("导入成功");
                } else {
                    this.$message.error("导入失败");
                }
                this.getAll();
            },
            submitUpload() {
                this.$refs.upload.submit();
            },
            handleRemove(file, fileList) {
                //移除已上传文件
                console.log("handleRemove:" + file, fileList);
            },
            handlePreview(file) {
                //点击已上传文件
                console.log("handlePreview:" + file);
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${file.name}？`);
            },
            lookTeach(row){
                /*//方法一
                this.request.get("/courses/teacher/" + row.id).then(res => {
                    if (res.flag) {
                        this.courses = res.data;
                    }else {
                        this.$message.error(res.msg);
                    }
                });
                this.vis = true;*/
                this.courses=row;
                this.vis = true;
            },
            lookChooseCourse(row){
                this.chooseCourses=row;
                this.chooseVis = true;
            },
        }
    }
</script>

<style scoped>

</style>